package com.accloud.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class NetworkUtils {
    public static void assertWifi(Context context) {
        if (((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo() == null) {
            throw new IllegalStateException("Wifi not available");
        }
    }

    public static boolean isWifiConnected(Context context) {
        NetworkInfo networkInfo = ((ConnectivityManager) context.getApplicationContext().getSystemService("connectivity")).getActiveNetworkInfo();
        if (networkInfo == null || networkInfo.getType() != 1 || !networkInfo.isConnected()) {
            return false;
        }
        return true;
    }

    public static String getSSID(Context context) {
        WifiInfo wifiInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
        if (wifiInfo == null) {
            return null;
        }
        String ssid = wifiInfo.getSSID();
        if (!ssid.startsWith("\"") || !ssid.endsWith("\"")) {
            return ssid;
        }
        return ssid.substring(1, ssid.length() - 1);
    }

    public static String getBSSID(Context context) {
        WifiInfo wifiInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
        if (wifiInfo == null) {
            return null;
        }
        return wifiInfo.getBSSID();
    }

    public static int getIpAddress(Context context) {
        WifiInfo wifiInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
        if (wifiInfo != null) {
            return wifiInfo.getIpAddress();
        }
        return 0;
    }

    public static void configWPAWifi(Context context, String ssid, String password) {
        WifiConfiguration configuration = new WifiConfiguration();
        configuration.SSID = String.format("\"%s\"", ssid);
        configuration.preSharedKey = String.format("\"%s\"", password);
        configuration.allowedAuthAlgorithms.set(0);
        configuration.allowedProtocols.set(0);
        configuration.allowedProtocols.set(1);
        configuration.allowedKeyManagement.set(1);
        configuration.allowedKeyManagement.set(2);
        configuration.allowedPairwiseCiphers.set(1);
        configuration.allowedPairwiseCiphers.set(2);
        configuration.allowedGroupCiphers.set(2);
        configuration.allowedGroupCiphers.set(3);
        configWifi(context, configuration);
    }

    private static void configWifi(Context context, WifiConfiguration configuration) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService("wifi");
        int networkId = addNetwork(configuration, wifiManager);
        wifiManager.disconnect();
        wifiManager.enableNetwork(networkId, true);
        wifiManager.reconnect();
    }

    private static int addNetwork(WifiConfiguration configuration, WifiManager wifiManager) {
        for (WifiConfiguration config : wifiManager.getConfiguredNetworks()) {
            if (config.SSID.equals(configuration.SSID)) {
                return config.networkId;
            }
        }
        return wifiManager.addNetwork(configuration);
    }
}
